﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

namespace Web.FrmSetting
{
    public partial class backupdb : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnServer_Click(object sender, EventArgs e)
        {
            string filePath = "d:\\BackUpDB\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day;
        
            string fullfilePath = filePath+ "\\GrainBankDB.bak";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("  BACKUP DATABASE GrainBankDB");
            strSql.Append("  TO disk = '" + fullfilePath + "'");

            if (SQLHelper.ExecuteNonQuery(strSql.ToString()) != 0)
            {
                //if (File.Exists(fullfilePath))
                //{
                //    File.SetAttributes(fullfilePath, FileAttributes.ReadOnly);//设置文件为只读文件
                //}
                lblMsg.Visible = true;
                lblMsg.ForeColor = System.Drawing.Color.Green;
                lblMsg.Text = "备份数据库成功!";
              
            }
            else
            {
                lblMsg.Visible = true;
                lblMsg.ForeColor = System.Drawing.Color.Red;
                lblMsg.Text = "备份数据库失败!";
            }
        }

        protected void btnClient_Click(object sender, EventArgs e)
        {
            lblMsg.Visible = true;
            lblMsg.ForeColor = System.Drawing.Color.Green;
            lblMsg.Text = "准备文件中，请稍候……!";
            string filePath = "d:\\BackUpDB\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day;

            string fullfilePath = filePath + "\\GrainBankDB.bak";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("  BACKUP DATABASE GrainBankDB");
            strSql.Append("  TO disk = '" + fullfilePath + "'");

            if (SQLHelper.ExecuteNonQuery(strSql.ToString()) != 0)
            {
                //if (File.Exists(fullfilePath))
                //{
                //    File.SetAttributes(fullfilePath, FileAttributes.ReadOnly);//设置文件为只读文件
                //}

                //将文件下载到客户端
                //Response.ContentType = "application/x-zip-compressed";
                //Response.AddHeader("Content-Disposition", "attachment;filename=GrainBankDB.bak");
                //string filename = fullfilePath;
                // Response.TransmitFile(filename);

                ZipFile(fullfilePath, filePath+ "\\GrainBankDB.rar");


                FileInfo fileInfo = new FileInfo(filePath+ "\\GrainBankDB.rar");
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "attachment;filename=GrainBankDB.rar");
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.ContentType = "application/octet-stream";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.WriteFile(fileInfo.FullName);
                Response.Flush();
                Response.End();
            }
            else
            {

            }
        }


       
            public void ZipFile(string strFile, string strZip)
            {
                if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
                    strFile += Path.DirectorySeparatorChar;
                ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
                s.SetLevel(6); // 0 - store only to 9 - means best compression
                zip(strFile, s, strFile);
                s.Finish();
                s.Close();
            }

            private void zip(string strFile, ZipOutputStream s, string staticFile)
            {
                if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
                Crc32 crc = new Crc32();
                string[] filenames = Directory.GetFileSystemEntries(strFile);
                string file = filenames[0];


                if (Directory.Exists(file))
                {
                    zip(file, s, staticFile);
                }

                else // 否则直接压缩文件
                {
                    //打开压缩文件
                    FileStream fs = File.OpenRead(file);

                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
                    ZipEntry entry = new ZipEntry(tempfile);

                    entry.DateTime = DateTime.Now;
                    entry.Size = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);

                    s.Write(buffer, 0, buffer.Length);
                }
            }


            private void zip2(string strFile, ZipOutputStream s, string staticFile)
            {
                if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
                Crc32 crc = new Crc32();
                string[] filenames = Directory.GetFileSystemEntries(strFile);
                foreach (string file in filenames)
                {

                    if (Directory.Exists(file))
                    {
                        zip(file, s, staticFile);
                    }

                    else // 否则直接压缩文件
                    {
                        //打开压缩文件
                        FileStream fs = File.OpenRead(file);

                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);
                        string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
                        ZipEntry entry = new ZipEntry(tempfile);

                        entry.DateTime = DateTime.Now;
                        entry.Size = fs.Length;
                        fs.Close();
                        crc.Reset();
                        crc.Update(buffer);
                        entry.Crc = crc.Value;
                        s.PutNextEntry(entry);

                        s.Write(buffer, 0, buffer.Length);
                    }
                }
            }



 
    
    }
}